package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    /**
     * 用户登录
     * @param session
     * @param employee
     * @return
     */
    @RequestMapping("/login")
    public R<Employee> login(HttpSession session, @RequestBody Employee employee){

        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<Employee>();
        lqw.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(lqw);

        if (emp == null){
            return R.error("用户名不存在");
        }

        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());


        if (! emp.getPassword().equals(password)){
            return R.error("用户密码错误");
        }

        if (emp.getStatus() == 0){
            return R.error("账号禁用");
        }

        session.setAttribute("employee",emp);
        return R.success(emp);
    }

    /**
     * 用户退出
     */
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        session.removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 用户添加
     * @param session
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> Save(HttpSession session,@RequestBody Employee employee){
        log.info("新增员工信息"+employee.toString());

//        对密码进行加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());

//        //获取域中对象
//        Employee emp = (Employee) session.getAttribute("employee");
//        employee.setCreateUser(emp.getId());
//        employee.setUpdateUser(emp.getId());

        //调用方法添加
        employeeService.save(employee);
        return R.success("添加员工成功");
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
   public R<Page> page(int page,int pageSize,String name){
        Page pageInfo = new Page(page,pageSize);

        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper();
        lqw.like(name != null,Employee::getName,name);
        lqw.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo,lqw);
        return R.success(pageInfo);
    }

    /**
     * 禁用员工账号
     * 修改员工信息
     * @param session
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpSession session,@RequestBody Employee employee){
        log.info(employee.toString());
//        Employee emp = (Employee)session.getAttribute("employee");
//        employee.setUpdateUser(emp.getId());
//        employee.setUpdateTime(LocalDateTime.now());
//        log.info(employee.toString());
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 回显信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getId(@PathVariable String id){
        log.info("根据id查询回显");
        Employee employee = employeeService.getById(id);
        if (employee != null){
            return R.success(employee);
        }
           return R.error("没有对应员工信息");
        }

}
